package itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import itheima.reggie.common.R;
import itheima.reggie.dto.OrdersDto;
import itheima.reggie.entity.OrderDetail;
import itheima.reggie.entity.Orders;
import itheima.reggie.service.OrderDetailService;
import itheima.reggie.service.OrdersService;
import itheima.reggie.service.UserService;
import itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    UserService userService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("1");
    }
    /*@GetMapping("/userPage")
    public R<Page<Orders>> userPage(int page,int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,id);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(pageInfo ,queryWrapper);


        return R.success(pageInfo);
    }*/
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPages(int page,int pageSize){
        Page<OrdersDto> pageInfo=new Page<>();
        Page<Orders> ordersPageInfo=new Page<>(page,pageSize);
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,id);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPageInfo ,queryWrapper);
        //将ordersPage中信息拷贝到DtoPage中
        BeanUtils.copyProperties(ordersPageInfo,page,"records");
        List<Orders> records = ordersPageInfo.getRecords();
        List<OrdersDto> ordersDtos = records.stream().map((item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        })).collect(Collectors.toList());
        pageInfo.setRecords(ordersDtos);
        return R.success(pageInfo);
    }
    @GetMapping("/page")
    public R<Page<OrdersDto>> queryPage(long page, long pageSize,String number,String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        wrapper.likeRight(number != null,Orders::getId,number);
        wrapper.orderByDesc(Orders::getCheckoutTime);
        wrapper.between(beginTime!=null&&endTime!=null,Orders::getCheckoutTime,beginTime,endTime);
        ordersService.page(pageInfo,wrapper);

        Page<OrdersDto> info=new Page<>(page,pageSize);
        BeanUtils.copyProperties(pageInfo,info,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> collect = records.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            ordersDto.setUserName(userService.getById(item.getUserId()).getName());
            return ordersDto;
        }).collect(Collectors.toList());
        info.setRecords(collect);
        return R.success(info);
    }
    //更改订单状态
    @PutMapping()
    public R<String> changeOrderStatus(@RequestBody Orders orders){
//        Orders byId = ordersService.getById(orders.getId());
//        byId.setStatus(orders.getStatus());
//        ordersService.updateById(byId);
        ordersService.updateById(orders);
        return R.success("修改成功！");
    }
    //再来一单
    @PostMapping("/again")
    public R<String> again(){
        //TODO
        return R.error("功能开发中！");
    }
}
